package cyber2d.core.objects;

//cyber2d
import cyber2d.core.PlatformerLevel;
//slick
import org.newdawn.slick.Image;
import org.newdawn.slick.SpriteSheet;
//phys2d
import net.phys2d.math.Vector2f;
import net.phys2d.raw.World;


/*
 * Object that have control, able to die
 * 
 * @author Meraxupypr (Konstantin Petrukhnov)
 */
public class Creature extends UniverseObject implements Controllable
{
	PlatformerLevel level;
	
	//Inventory inventory;
	//Skills skills;
	//Doll doll;
	public int hpMax; //maximum hitpoints based on creature stats
	public int hpCur; //current hitpoints (can be over 'hpMax' if affected by some bonuses)
	
	//if creature collide for something at bottom (uses for jumping)
	//number of bottom collisions (if 2 or more allow jump)
	public int nOfBottomCollisions; 
	
	boolean wantJump; //object want jump
	boolean wantShoot; //object want shoot
	int direction; //object want move to: -1 left, 0 stay, 1 right
	int lookAt; //for object interaction and drawing sprites; can be 1 or -1 (1 means right)
	
	int curVelocity;
	int maxVelocity;
	
	//die
	public boolean dead;
	
	
	//weapon type
	float rateOfFire; //how fast can shoot in miliseconds
	long lastShotTime; //when last shot was fired
	
	
	//render
	SpriteSheet sprites; //collection of sprites for image
	
	public Creature(World w, PlatformerLevel level, SpriteSheet sprites)
	{
		super(w);
		nOfBottomCollisions = 0;
		this.level = level;
		
		rateOfFire = 1000.0f; //1 second
		lastShotTime = System.currentTimeMillis();
		
		lookAt = 1;
		dead = false;
		
		this.sprites = sprites;
	}
	
	//moving+action in world + AI
	public void update()
	{
		//here goes AI
		think();
		
		//side mooving
		body.addForce(new Vector2f(direction*300,0));
		//if velocity is to high - reduce it
		
		
			//make jump if need
		//IF collide bottom AND want jump
		if((nOfBottomCollisions>0)&&(wantJump))
		{
			//System.out.println("jump!");
			body.addForce(new Vector2f(0,-300000)); //jump
		
		    nOfBottomCollisions = 0;
		 //reset bottom collisions
		}
		
		
		//if want - make shot (check time delay)
		if ((wantShoot)&((System.currentTimeMillis()-lastShotTime)>rateOfFire))
		{
			lastShotTime = System.currentTimeMillis();
			makeShot();
			
		}
		
	}
	
	//creatures mind
	public void think()
	{
		if (this!=level.player) //if creature is not player
		{
			wantShoot = true;
		}
	}
	
	//invokes when creature die (hpCur goes bellow zero)
	public void die()
	{
		//mark as dead
		dead = true;
		//remove body from world
		world.remove(this.body);
	}

	/* (non-Javadoc)
	 * @see cyber2d.core.objects.Controllable#wantJump(boolean)
	 */
	public void wantJump(boolean wantJump)
	{
		this.wantJump = wantJump;
	}

	/* (non-Javadoc)
	 * @see cyber2d.core.objects.Controllable#wantShoot(boolean)
	 */
	public void wantShoot(boolean wantShoot)
	{
		// TODO Auto-generated method stub
		this.wantShoot = wantShoot;
	}

	/* (non-Javadoc)
	 * @see cyber2d.core.objects.Controllable#wantSide(int)
	 */
	public void wantDirection(int direction)
	{
		// change direction for moovment
		this.direction += direction;
		// change lookAt
		if (this.direction != 0)
		{
			lookAt = this.direction;
		}
	}

	//make shot
	public void makeShot()
	{
		//define position
		float shotPosX = body.getPosition().getX()+(body.getShape().getBounds().getWidth()/2+1)*lookAt;
		//float shotPosX = body.getPosition().getX() + 40*lookAt;
		float shotPosY = body.getPosition().getY();
			
		
		//add shot to cur position in next direction
		level.addShot("9mm", shotPosX, shotPosY, lookAt);
	}

	//image for render
	public Image getImage()
	{
		if (lookAt == 1)
		{
			return sprites.getSprite(0, 0);
		}
		else 
		{
			return sprites.getSprite(0,0).getFlippedCopy(true, false);
		}
	}
}
